// pages/workOrder/orderComplete/index.js
import urls from '../../../requestUrl'
import request from '../../../baseRequest'



Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentIndex: 0,
        fullDetail: [],
        showRightPop: false,
        showBottomPop: false,
        showOverLay: true,
        orderSource: '',
        orderNo: '',
        mileageList: [],
        mileageParam: {
            count: 0
        },
        mileageNameList: ['到达现场', '到达目的地', '作业返程']
    },
    /**获取工单所有表单数据 */
    getOrderFullDetail() {
        let url = `${urls.workOrder.getFullDetail}/${this.data.orderId}`
        request.getRequest(url, {}, (res) => {
            console.log(res)
            this.setData({
                fullDetail: res.workOrderFormList,
                ['mileageList[0]']: res.reachKm,
                ['mileageList[1]']: res.endKm,
                ['mileageList[2]']: res.returnKm
            })
            this.getScrollHeight()
        }, () => {})
    },
    /**根据Class获取当前组件高度 */
    getLayoutHeightByClass(_className, call) {

        wx.createSelectorQuery().select(_className).boundingClientRect(res => {
            console.log('boundingClientRect', res)
            typeof call === 'function' && call(res.height)
        }).exec()
    },
    /**获取滚动组件高度 */
    getScrollHeight() {
        let wHeight;
        wx.getSystemInfo({
            success: res => {
                console.log('sysInfo', res)
                wHeight = res.windowHeight
                this.getLayoutHeightByClass('#topLayout', (height) => {
                    console.log('height', height)
                    this.setData({
                        scrollHeight: wHeight - height - 2
                    })
                })
            }
        })

    },
    /**切换右侧弹出层显示 */
    toggleRightPopup() {
        let isShow = !this.data.showRightPop
        this.setData({
            showRightPop: isShow
        })
    },

    /**切换底部弹出层显示 */
    toggleBottomPopup() {
        let isShow = !this.data.showBottomPop
        this.setData({
            showBottomPop: isShow
        })
    },

    /**切换显示阶段 */
    handleChangeItem(e) {
        let _index = parseInt(e.currentTarget.id)
        this.setData({
            currentIndex: _index,
            scrollTop: 0,
        })
        this.toggleRightPopup()
    },
    /**切换下一阶段 */
    handleToNextProcess() {
        this.setData({
            currentIndex: this.data.currentIndex + 1,
            scrollTop: 0,
        })
    },

    /**显示底部弹出层 */
    handleShowBottomPop(e) {
        let index = e.currentTarget.id
        this.data.mileageParam.count = 0
        this.setData({
            ['mileageParam.index']: index,
            showBottomPop: true
        })
    },

    /**监听修改里程数 */
    handleChangeMileageCount(e) {
        let _count = e.detail
        this.data.mileageParam.count = _count
    },

    /**触发修改按钮 */
    handleSubmitUpadeMileage() {
        wx.showModal({
            title: '温馨提示',
            content: '是否确认修改里程数数据？',
            confirmText: '确认修改',
            cancelText: '取消',
            success: res => {
                if (res.confirm) {
                    this.updateMileageRequest()
                }
            }
        })

    },

    /**更新里程数请求 */
    updateMileageRequest() {
        let param = {}
        switch (this.data.mileageParam.index) {
            case '0':
                param.reachKm = this.data.mileageParam.count
                break;
            case '1':
                param.endKm = this.data.mileageParam.count
                break;
            case '2':
                param.returnKm = this.data.mileageParam.count
                break;
        }
        let url = `${urls.workOrder.updateMileage}/${this.data.orderId}`
        request.postRequest(url, param, (res) => {
            wx.showModal({
                title: '温馨提示',
                content: '里程数修改成功',
                confirmText: '关闭',
                showCancel: false,
                success: res => {
                    this.getOrderFullDetail()
                    this.toggleBottomPopup()
                }
            })
        }, () => {})
    },

    /**查看五方位图 */
    toVehicleStatusPage(e) {
        let d = e.currentTarget.dataset
        getApp().saveVehicleStatus(d.name, d.value)
        wx.navigateTo({
            url: `/pages/vehicleStatus/index?name=${d.name}&disabled=true`
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        // this.data.orderId = options.oid || ''
        this.setData({
            orderSource: options.from,
            orderNo: options.ono,
            orderId: options.oid || ''
        })
        this.getOrderFullDetail()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage: function() {

    // }
})